package com.ymk.config;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CmdConfig {

    public static void cmdTMain(String cmdName,String cmdCode) throws IOException, TemplateException {
        String rootUrl = System.getProperty("user.dir");
        // 路径根据自己项目的特点调整
        String rootPath = rootUrl+"\\modules\\module-gocder\\src\\main\\java";
        String packageName = "com.ymk.cmd";
        String templatePath = rootUrl+"\\modules\\module-gocder\\src\\main\\resources\\templates";

        String templateName = "cmd.ftl";
        String templateCodeName = "cmdCode.ftl";

        JavaProperties javaGet = new JavaProperties(cmdName,"Get",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateName, javaGet);

        JavaProperties javaList = new JavaProperties(cmdName,"List",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateName, javaList);

        JavaProperties javaPost = new JavaProperties(cmdName,"Post",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateName, javaPost);

        JavaProperties javaPatch = new JavaProperties(cmdName,"Patch",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateName, javaPatch);

        JavaProperties javaDel = new JavaProperties(cmdName,"Del",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateName, javaDel);

        JavaProperties javaCode = new JavaProperties(cmdName,"CmdCode",cmdCode, packageName);
        autoCodingJavaEntity(rootPath, templatePath, templateCodeName, javaCode);
    }

    /**
     * 简单的代码生成器.
     *
     * @param rootPath       maven 的  java 目录
     * @param templatePath   模板存放的文件夹
     * @param templateName   模板的名称
     * @param javaProperties 需要渲染对象的封装
     * @throws IOException       the io exception
     * @throws TemplateException the template exception
     */
    public static void autoCodingJavaEntity(String rootPath,
                                            String templatePath,
                                            String templateName,
                                            JavaProperties javaProperties) throws IOException, TemplateException {

        // freemarker 配置
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

        configuration.setDefaultEncoding("UTF-8");
        // 指定模板的路径
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        // 根据模板名称获取路径下的模板
        Template template = configuration.getTemplate(templateName);
        // 处理路径问题
        final String ext = ".java";
        String javaName;
        if(javaProperties.getType().equals("CmdCode")){
            javaName = javaProperties.getCmdName()+"CmdCode".concat(ext);
        }else{
            javaName = javaProperties.getCmdName()+javaProperties.getType()+"Cmd".concat(ext);
        }

        String packageName = javaProperties.getPkg();

        String out = rootPath.concat(Stream.of(packageName.split("\\."))
                .collect(Collectors.joining("/", "/", "/" + javaName)));

        // 定义一个输出流来导出代码文件
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(out));
        // freemarker 引擎将动态数据绑定的模板并导出为文件
        template.process(javaProperties, outputStreamWriter);

    }
}
